﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraRichEdit;
using DevExpress.XtraTab;
using System.IO;
using DevExpress.XtraGrid.Columns;
using DevExpress.Data.Filtering;

namespace QuanLyKho
{
    public partial class frmHopDong : Form
    {
        private readonly TaiKhoan taiKhoan = new TaiKhoan("admin", "admin", "admin");
        private int soBarAn;
        public frmHopDong()
        {
            InitializeComponent();
        }

        private void themTrangHopDong(string fileName)
        {
            string tenFile = Path.GetFileNameWithoutExtension(fileName);
            // 
            // Thêm rich edit control để đọc file hợp đồng
            // 
            RichEditControl richEditControl1 = new RichEditControl() { Location = new Point(4, 4), Name = tenFile, Size = new Size(637, 389), TabIndex = 0, ReadOnly = true };
            richEditControl1.Options.DocumentCapabilities.CharacterFormatting = DocumentCapability.Disabled;
            richEditControl1.Options.DocumentCapabilities.CharacterStyle = DocumentCapability.Disabled;
            richEditControl1.Options.DocumentCapabilities.ParagraphFormatting = DocumentCapability.Disabled;
            richEditControl1.Options.DocumentCapabilities.ParagraphStyle = DocumentCapability.Disabled;
            richEditControl1.Options.Behavior.Copy = DocumentCapability.Disabled;
            richEditControl1.Options.Behavior.Paste = DocumentCapability.Disabled;
            richEditControl1.Options.Behavior.Cut = DocumentCapability.Disabled;
            richEditControl1.Options.Behavior.SaveAs = DocumentCapability.Disabled;
            richEditControl1.Options.Behavior.Save = DocumentCapability.Disabled;
            //
            //Thêm tab hiển thị file hợp đồng
            //
            XtraTabPage xtraTabPage1 = new XtraTabPage();
            xtraTabPage1.SuspendLayout();
            tabHopDong.TabPages.Add(xtraTabPage1);
            tabHopDong.SelectedTabPage = xtraTabPage1;
            xtraTabPage1.Controls.Add(richEditControl1);
            xtraTabPage1.Name = tenFile;
            xtraTabPage1.Size = new Size(644, 395);
            xtraTabPage1.Text = tenFile;
            xtraTabPage1.ResumeLayout(false);
            //
            // Điểu khiển thêm dữ liệu
            //
            richEditControl1.Dock = DockStyle.Fill;
            richEditControl1.LoadDocument(fileName);
        }

        private void frmHopDong_Load(object sender, EventArgs e)
        {
            dateTuNgay.Text = DateTime.MinValue.Date.ToShortDateString();
            dateDenNgay.Text = DateTime.Now.Date.ToShortDateString();
            hienThiDanhSachHopDong();
            soBarAn = barHienThi.ItemLinks.Count;
        }

        private void hienThiDanhSachHopDong()
        {
            DataSet ds = connection.FillDataSet("proc_getDanhSachHopDong", CommandType.StoredProcedure, null, null, taiKhoan);

            gctHopDong.DataSource = ds.Tables[0];
            gclMaHopDong.FieldName = ds.Tables[0].Columns["MaHopDong"].ColumnName;
            gclNgayLap.FieldName = ds.Tables[0].Columns["NgayLap"].ColumnName;
            gclTenDoiTac.FieldName = ds.Tables[0].Columns["DoiTac"].ColumnName;
        }

        private void gctHopDong_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                string fileName = gvHopDong.GetDataRow(gvHopDong.GetSelectedRows()[0])["path"].ToString();
                bool isNotExits = true;
                for (int i = 0; i < tabHopDong.TabPages.Count; i++)
                {
                    if (Path.GetFileNameWithoutExtension(fileName) == tabHopDong.TabPages[i].Name)
                    {
                        tabHopDong.SelectedTabPage = tabHopDong.TabPages[i];
                        isNotExits = false;
                        break;
                    }
                }
                if (isNotExits)
                    themTrangHopDong(fileName);
            }
            catch
            {
                MessageBox.Show("Không thể hiển thị hợp đồng","Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        private void tabHopDong_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                try
                {
                    if (tabHopDong.TabPages.Count > 0)
                    {
                        ContextMenu m = new ContextMenu();
                        m.MenuItems.Add(new MenuItem("Đóng tab", menu_DongTab));
                        m.MenuItems.Add(new MenuItem("Đóng tất cả các tab", menu_DongTatCaCacTab));
                        m.MenuItems.Add(new MenuItem("Đóng tất cả các tab khác", menu_DongTatCaCacTabKhac));
                        m.Show(tabHopDong, new Point(e.X, e.Y));
                    }
                }
                catch { }
            }
            else if (e.Button == MouseButtons.Middle)
                menu_DongTab(sender, e);
        }

        private void menu_DongTab(object sender, EventArgs e)
        {
            tabHopDong.TabPages.Remove(tabHopDong.SelectedTabPage);
        }

        private void menu_DongTatCaCacTab(object sender, EventArgs e)
        {
            for (int i = 0; i < tabHopDong.TabPages.Count; i++)
            {
                tabHopDong.TabPages.RemoveAt(i);
            }
            tabHopDong.TabPages.Remove(tabHopDong.SelectedTabPage);
        }

        private void menu_DongTatCaCacTabKhac(object sender, EventArgs e)
        {
            for (int i = 0; i < tabHopDong.TabPages.Count; i++)
            {
                tabHopDong.TabPages.RemoveAt(i);
            }
        }

        private void txtMaHopDong_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (txtMaHopDong.Text.Trim() == "")
                {
                    gvHopDong.Columns["MaHopDong"].ClearFilter();
                    picCleanMaHopDong.Visible = false;
                }
                else
                {
                    gvHopDong.Columns["MaHopDong"].FilterInfo = new ColumnFilterInfo("Contains([MaHopDong], '" + txtMaHopDong.Text.Trim() + "')");
                    picCleanMaHopDong.Visible = true;
                }
            }
            catch { }
        }

        private void txtTenDoiTac_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (txtTenDoiTac.Text.Trim() == "")
                {
                    gvHopDong.Columns["DoiTac"].ClearFilter();
                    picCleanTenDoiTac.Visible = false;
                }
                else
                {
                    gvHopDong.Columns["DoiTac"].FilterInfo = new ColumnFilterInfo("Contains([Đối tác], '" + XuLy.convertToUnSign3(txtTenDoiTac.Text.Trim()) + "')");
                    picCleanTenDoiTac.Visible = true;
                }
            }
            catch { }
        }
        
        private void dateEdit_EditValueChanged(object sender, EventArgs e)
        {
            if (sender == dateTuNgay)
            {
                if (dateTuNgay.DateTime.Date == DateTime.MinValue.Date)
                    picCleanTuNgay.Visible = false;
                else
                    picCleanTuNgay.Visible = true;
                dateDenNgay.Properties.MinValue = dateTuNgay.DateTime.Date;
            }
            else if (sender == dateDenNgay)
            {
                if (dateDenNgay.DateTime.Date == DateTime.Now.Date)
                    picCleanDenNgay.Visible = false;
                else
                    picCleanDenNgay.Visible = true;
                dateTuNgay.Properties.MaxValue = dateDenNgay.DateTime.Date;
            }

            try
            {
                CriteriaOperator filter = CriteriaOperator.Parse("[NgayLap] >= ? and [NgayLap] <= ?", dateTuNgay.DateTime.Date, dateDenNgay.DateTime.Date);
                gvHopDong.Columns["NgayLap"].FilterInfo = new ColumnFilterInfo(filter.ToString());
            }
            catch { }
        }

        private void picClean(object sender, EventArgs e)
        {
            if (sender == picCleanMaHopDong)
                txtMaHopDong.Text = "";
            else if (sender == picCleanTenDoiTac)
                txtTenDoiTac.Text = "";
            else if (sender == picCleanTuNgay)
                dateTuNgay.Text = DateTime.MinValue.Date.ToShortDateString();
            else if (sender == picCleanDenNgay)
                dateDenNgay.Text = DateTime.Now.Date.ToShortDateString();
            else if (sender == picCleacAll)
            {
                txtMaHopDong.Text = "";
                txtTenDoiTac.Text = "";
                dateTuNgay.Text = DateTime.MinValue.Date.ToShortDateString();
                dateDenNgay.Text = DateTime.Now.Date.ToShortDateString();
            }
        }

        private void dpnTimKiem_ClosedPanel(object sender, DevExpress.XtraBars.Docking.DockPanelEventArgs e)
        {
            barHienThi.Visible = true;
            barChkTimKiem.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
            soBarAn--;
        }

        private void dpnDanhSachHopDong_ClosedPanel(object sender, DevExpress.XtraBars.Docking.DockPanelEventArgs e)
        {
            barHienThi.Visible = true;
            barChkDanhSachHopDong.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
            soBarAn--;
        }

        private void barChkTimKiem_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (barChkTimKiem.Checked)
            {
                dpnTimKiem.Show();
                barChkTimKiem.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                barChkTimKiem.Checked = false;
                soBarAn++;
            }
            if (soBarAn == barHienThi.ItemLinks.Count)
                barHienThi.Visible = false;
        }

        private void barChkDanhSachHopDong_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (barChkDanhSachHopDong.Checked)
            {
                dpnDanhSachHopDong.Show();
                barChkDanhSachHopDong.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                barChkDanhSachHopDong.Checked = false;
                soBarAn++;
            }
            if (soBarAn == barHienThi.ItemLinks.Count)
                barHienThi.Visible = false;
        }
    }
}
